package controllers

import (
	"hapi/models"
	// "encoding/json"
	"fmt"
	"github.com/astaxie/beego"
)

// Operations about Tokens
type TokenController struct {
	beego.Controller
}

// @Title Token
// @Description Logs user into the system
// @Param	username		query 	string	true		"The username for token"
// @Param	password		query 	string	true		"The password for token"
// @Success 200 {string} the jwt string
// @Failure 403 user not exist
// @router /token [get]
func (u *TokenController) Token() {
	u.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	username := u.GetString("username")
	password := u.GetString("password")
	fmt.Println(username, password)
	token, err := models.GetToken(username, password)
	if err == nil {
		u.Data["json"] = token
	} else {
		u.Data["json"] = err.Error()
	}
	
	u.ServeJSON()
}
